<?php
/**
 * DownloadExcel
 * - Download Excel
 * @author Longlt
 * $Id: $
 */

class DownloadExcel extends DownloadExcelExe{
	// Data controll for output excel
	public $listControl = array(
		"report" => array(
			"fileTpl" 		=> 'report_box',
			"fileOutput" 	=> 'report',
			"sheet" 		=> "Sheet1",
		),
		"announcementList001" => array(
			"fileTpl" 		=> 'announcement_list_001',
			"fileOutput" 	=> 'announcement_list_001',
			"sheet" 		=> "Sheet1",
		),
		"announcementList002" => array(
			"fileTpl" 		=> 'announcement_list_002',
			"fileOutput" 	=> 'announcement_list_002',
			"sheet" 		=> "Sheet1",
		),
		"announcementList003" => array(
			"fileTpl" 		=> 'announcement_list_003',
			"fileOutput" 	=> 'announcement_list_003',
			"sheet" 		=> "Sheet1",
		),
		"member" => array(
			"fileTpl" 		=> 'member_001',
			"fileOutput" 	=> 'member_001',
			"sheet" 		=> "Sheet1",
		),
	);

	public function run($action, $dataDynamic, $totalDynamic = null) {
		parent::CreateByListControl($action, $dataDynamic, $totalDynamic = null);
	}

	/**
	  * visitingRecordList001
	  * - Drawn excel for dynamic data visiting_record_list_001
	  * @return object $objPHPExcel
	  * @author Longlt
	 */
	public static function report() {
		list($objPHPExcel, $dataDynamic) = func_get_args();
		/**
		 * Fill data dynamic
		 **/
		$objPHPExcel->getActiveSheet()->SetCellValue('B16', 'Phí dịch vụ lưu trữ hồ sơ');
            $objPHPExcel->getActiveSheet()->SetCellValue('D16', $dataDynamic[0]);
            $objPHPExcel->getActiveSheet()->SetCellValue('E16', $dataDynamic[1]);
            $objPHPExcel->getActiveSheet()->SetCellValue('F16', '$contractPrice');
//		// Fix Template
//	    if ($i != '') {
//	    	$k = ($i);
//	    	$totalDynamic = ($count - $count % 50 + 50) + 2;
//
//	    	//$objPHPExcel->getActiveSheet()->removeRow($k, $totalDynamic - $k + 1);
//	    }
		/**
		 * Fill data static
		 **/
		// Code here

		return $objPHPExcel;
	}

	/**
	  * announcementList001
	  * - Drawn excel for dynamic data announcement_list_001
	  * @return object $objPHPExcel
	  * @author XuyenHTK
	 */
	public static function announcementList001() {
		list($objPHPExcel, $dataDynamic) = func_get_args();

		/**
		 * Fill data dynamic
		 **/
		$announcement = $dataDynamic["announcement"];
		$assign = $dataDynamic["assign"];
		//$views = $dataDynamic["views"];
		$count = count($announcement);

		$eventType = Util::itemAlias('Event');
		$statusType = Util::itemAlias('Status');

		for ($i = 2,$j = 0; $j < $count; $i++, $j++){
			// Fill Data
			$objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $j + 1);
			$objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $announcement[$j]['title']);
			$objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $eventType[$announcement[$j]['event_fg']]);
			$objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $statusType[$announcement[$j]['status']]);

			$announcement_id = $announcement[$j]['id'];
			$objPHPExcel->getActiveSheet()->setCellValue('E' . $i, isset($assign[$announcement_id]['count_assign']) ? $assign[$announcement_id]['count_assign'] : '');
			$objPHPExcel->getActiveSheet()->setCellValue('F' . $i, isset($assign[$announcement_id]['count_view']) ? $assign[$announcement_id]['count_view'] : '');
			$objPHPExcel->getActiveSheet()->setCellValue('G' . $i, isset($assign[$announcement_id]['count_no_reply']) ? $assign[$announcement_id]['count_no_reply'] : '');
			$objPHPExcel->getActiveSheet()->setCellValue('H' . $i, isset($assign[$announcement_id]['count_purpose_join']) ? $assign[$announcement_id]['count_purpose_join'] : '');
			$objPHPExcel->getActiveSheet()->setCellValue('I' . $i, isset($assign[$announcement_id]['count_joined']) ? $assign[$announcement_id]['count_joined'] : '');

			$objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(-1);
		}

//		// Fix Template
	    if ($i != '') {
	    	$k = ($i);
	    	$totalDynamic = ($count - $count % 50 + 50) + 2;

	    	$objPHPExcel->getActiveSheet()->removeRow($k, $totalDynamic - $k + 1);
	    }
		/**
		 * Fill data static
		 **/
		// Code here

		return $objPHPExcel;
	}

	public static function announcementList002() {
		list($objPHPExcel, $dataDynamic) = func_get_args();

		/**
		 * Fill data dynamic
		 **/
		$announcement 		= $dataDynamic["announcement"];
		$assign 			= $dataDynamic["assign"];
		$count				= 0;

		$purposeJoin 		= Util::itemAlias('EvenJoin');
		$announcementType 	= Util::itemAlias('Event');
		$announcement_id	= $announcement["id"];
		$i = 5;
		$j = 0;

		$objPHPExcel->getActiveSheet()->setCellValue('A1', $announcementType[$announcement["event_fg"]]);
		$objPHPExcel->getActiveSheet()->setCellValue('A2', $announcement["title"]);
		if (isset($assign[$announcement_id]) && !empty($assign[$announcement_id])) {
			$count	= count($assign[$announcement_id]);

			for ($j = 0; $j < $count; $j++, $i++) {
				$assignData = $assign[$announcement_id][$j];
				$objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $j + 1);
				$objPHPExcel->getActiveSheet()->setCellValue('B' . $i, Util::formatFullName($assignData['fullname']));
				$objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $assignData['code']);
				$objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $assignData['coursename']);
				$objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $assignData['view_fg'] == 1 ? 'X' : '');
				if ($announcement['event_fg'] == 1) {

					$objPHPExcel->getActiveSheet()->setCellValue('F' . $i, isset($assignData['purpose_join']) ? $purposeJoin[$assignData['purpose_join']] : '');
					$objPHPExcel->getActiveSheet()->setCellValue('G' . $i, isset($assignData['event_joined']) && $assignData['event_joined'] == 1 ? 'X' : '');
					$objPHPExcel->getActiveSheet()->setCellValue('H' . $i, isset($assignData['comment']) ? $assignData['comment'] : '');
				}
				$objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(-1);
			}
		}

//		// Fix Template
	    if ($i != '') {
	    	$k = ($i);
	    	$count = $j - 1;
	    	$totalDynamic = ($count - $count % 50 + 50);
	    	$objPHPExcel->getActiveSheet()->removeRow($k, $totalDynamic);
	    }
		/**
		 * Fill data static
		 **/
		// Code here

		return $objPHPExcel;
	}

	public static function announcementList003() {
		list($objPHPExcel, $dataDynamic) = func_get_args();

		/**
		 * Fill data dynamic
		 **/
		$announcement 		= $dataDynamic["announcement"];
		$assign 			= $dataDynamic["assign"];
		$count 				= count($announcement);

		$purposeJoin 		= Util::itemAlias('EvenJoin');
		$announcementType 	= Util::itemAlias('Event');
		$i = 2;

		for ($j = 0; $j < $count; $j++){
			$announcement_id = $announcement[$j]['id'];
			$startRow = $endRow = 0;
			if (isset($assign[$announcement_id]) && !empty($assign[$announcement_id])) {
				$startRow = $endRow = $i;
				foreach ($assign[$announcement_id] as $eachMember) {
					// Fill Data
					$objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $i - 1);
					$objPHPExcel->getActiveSheet()->setCellValue('D' . $i, isset($eachMember['fullname']) ? Util::formatFullName($eachMember['fullname']) : '');
					$objPHPExcel->getActiveSheet()->setCellValue('E' . $i, isset($eachMember['code']) ? $eachMember['code'] : '');
					$objPHPExcel->getActiveSheet()->setCellValue('F' . $i, isset($eachMember['coursename']) ? $eachMember['coursename'] : '');
					$objPHPExcel->getActiveSheet()->setCellValue('G' . $i, isset($eachMember['view_fg']) && $eachMember["view_fg"] !== null ? 'X' : '');
					if ($announcement[$j]['event_fg'] == 1) {
						$objPHPExcel->getActiveSheet()->setCellValue('H' . $i, isset($eachMember['purpose_join']) ? $purposeJoin[$eachMember['purpose_join']] : '');
						$objPHPExcel->getActiveSheet()->setCellValue('I' . $i, isset($eachMember['event_joined']) && $eachMember['event_joined'] == 1 ? 'X' : '');
						$objPHPExcel->getActiveSheet()->setCellValue('J' . $i, isset($eachMember['comment']) ? $eachMember['comment'] : '');
					}

					$objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(-1);
					$i++;
					$endRow++;
				}
				$objPHPExcel->setActiveSheetIndex(0)->mergeCells('B' . $startRow . ':B' . ($endRow - 1));
				$objPHPExcel->setActiveSheetIndex(0)->mergeCells('C' . $startRow . ':C' . ($endRow - 1));
				$objPHPExcel->getActiveSheet()->setCellValue('B' . $startRow, $announcement[$j]['title']);
				$objPHPExcel->getActiveSheet()->setCellValue('C' . $startRow, $announcementType[$announcement[$j]['event_fg']]);
			}
		}

//		// Fix Template
	    if ($i != '') {
	    	$k = ($i);
	    	$totalDynamic = ($count - $count % 50 + 50) + 2;

	    	$objPHPExcel->getActiveSheet()->removeRow($k, $totalDynamic - $k + 1);
	    }
		/**
		 * Fill data static
		 **/
		// Code here

		return $objPHPExcel;
	}

	/**
	  * member_001
	  * - Drawn excel for dynamic data member_001
	  * @return object $objPHPExcel
	  * @author Longlt
	 */
	public static function member() {
		list($objPHPExcel, $dataDynamic) = func_get_args();

		$purposeJoin = Util::itemAlias('EvenJoin');
		$announcements = $dataDynamic["announcements"];
		$start = $dataDynamic["start"];
		$end = $dataDynamic["end"];
		$i = 16;
		$j = 1;

		/**
		 * Fill data dynamic
		 **/
		if ($start == "" && $end != "") {
			$objPHPExcel->getActiveSheet()->SetCellValue('A2', "Đến " . $end);
		}
		elseif ($start != "") {
			$time = "Từ " . $start;
			$time .= ($end == "") ? "" : " đến " . $end;
			$objPHPExcel->getActiveSheet()->SetCellValue('A2', $time);
		}
		$objPHPExcel->getActiveSheet()->SetCellValue('C4', Util::formatFullName($dataDynamic["member"]->fullname));
		$objPHPExcel->getActiveSheet()->SetCellValue('C5', Util::formatPhone($dataDynamic["member"]->phone));
		$objPHPExcel->getActiveSheet()->SetCellValue('C6', $dataDynamic["member"]->email);
		$objPHPExcel->getActiveSheet()->SetCellValue('C7', $dataDynamic["courseActive"]->id);

		$objPHPExcel->getActiveSheet()->SetCellValue('C9', 2);
		$objPHPExcel->getActiveSheet()->SetCellValue('C10', $dataDynamic['asigned']);
		$objPHPExcel->getActiveSheet()->SetCellValue('C11', $dataDynamic['absence']);
		$objPHPExcel->getActiveSheet()->SetCellValue('C12', $dataDynamic['joined']);


		$i = 16;
		$j = 1;
		foreach($announcements as $announcement) {
			$objPHPExcel->getActiveSheet()->SetCellValue('A' . $i, $j);
			$objPHPExcel->getActiveSheet()->SetCellValue('B' . $i, $announcement->title);
			$objPHPExcel->getActiveSheet()->SetCellValue('C' . $i, isset($announcement->member_asign->view_fg) && $announcement->member_asign->view_fg == 1 ? "X" : "O");
			$objPHPExcel->getActiveSheet()->SetCellValue('D' . $i, isset($announcement->member_asign->purpose_join) ? $purposeJoin[$announcement->member_asign->purpose_join] : "");
			$objPHPExcel->getActiveSheet()->SetCellValue('E' . $i, isset($announcement->member_asign->event_joined) && $announcement->member_asign->event_joined == 1 ? "X" : "O");
			$objPHPExcel->getActiveSheet()->SetCellValue('F' . $i, isset($announcement->member_asign->comment) ? $announcement->member_asign->comment : "");
			$objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(-1);
			$i++;
			$j++;
		}
		// Fix Template
	    //if ($j != 1) {
	    	$k = ($i);
	    	$count = $j - 1;
	    	$totalDynamic = ($count - $count % 50 + 50);
	    	$objPHPExcel->getActiveSheet()->removeRow($k, $totalDynamic);
	    //}
		/**
		 * Fill data static
		 **/
		// Code here

		return $objPHPExcel;
	}
}
?>